package com.qf.controller;

import com.qf.pojo.Employee;
import com.qf.service.EmployeeService;
import com.qf.utils.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/*员工信息管理后台*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @RequestMapping("/regist")
    public String regist(@RequestBody Map map){

        String name =(String) map.get("name");
        String password = (String) map.get("password");
        Employee employee = employeeService.selectOne(name);
        if(name!=employee.getName()){
            int regist = employeeService.regist(name, password);
            if (regist==1){
                return "success";
            }
        }
            return "error";

    }
    /*查询所有*/
    @RequestMapping("/findAll")
    public List<Employee> selectAll(){


        Employee employee=new Employee();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳



        List<Employee> employees = employeeService.selectAll();
        for (int i=0;i<employees.size();i++){
            employees.get(i).setCreateTime(date);
            employees.get(i).setCreatePerson("赵四");
        }
        return employees;
    }
    /*根据id查询*/
    @RequestMapping("/findById")
    public Employee selectOne(@RequestBody Map map){
        String id = (String) map.get("id");

        System.out.println(id);

        Employee employee = employeeService.selectOne(id);
        System.out.println(employee.toString());
        return employee;
    }
    /*删除*/
    @RequestMapping("/delete")
    public String delete(@RequestBody Map map){
        List<String> list=new ArrayList<>();
        String empId = (String) map.get("empId");
        System.out.println(empId);
        list.add(empId);
        int res = employeeService.deleteOne(list);
        if(res==1){
            return "success";
        }
        return "error";
    }
/*增加*/
  @RequestMapping("/insert")
    public String insert(@RequestBody Employee employee){
        int res = employeeService.insertOne(employee);
            System.out.println(res);
        if(res==1){
            return "success";
        }
        return "error";
    }
/*修改*/
    @RequestMapping("/update")
    public String update(@RequestBody Employee employee){
        int res = employeeService.update(employee);
        if(res==1){
            return "success";
        }
        return "error";
    }

    /*上传*/
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile){
        UploadUtils uploadUtils = new UploadUtils();
        String upload = uploadUtils.upload(multipartFile);
        return upload;
    }
}
